package com.example.baselibrary.Utils;

import android.content.Context;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.baselibrary.Base.MyApplication;
import com.example.baselibrary.R;

public class SelfToastUtil extends Toast {

    private static SelfToastUtil instance;

    private Toast mToast;
    private TextView mTextView;
    private String message;
    private Handler mHandler = new Handler();
    private boolean canceled = true;

    public static synchronized SelfToastUtil getInstance() {
        if (instance == null) {
            instance = new SelfToastUtil(MyApplication.getAppContext());
        }
        return instance;
    }

    public SelfToastUtil(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //自定义布局
        View view = inflater.inflate(R.layout.toast_self, null);
        //自定义toast文本
        mTextView = (TextView) view.findViewById(R.id.tv_toast);
        if (mToast == null) {
            mToast = new Toast(context);
        }
        //设置toast居中显示
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(view);
    }

    /**
     * 自定义居中显示toast
     */
    public void show(String s) {
//        if (mToast != null) {
//            mToast.cancel();
//            mToast = null;
//        }
        mTextView.setText(s);
        mToast.show();
    }

}